package com.zhanghu.backendZray.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhanghu.backendZray.exception.ApiException;
import com.zhanghu.backendZray.pojo.dto.ApiResponse;
import com.zhanghu.backendZray.pojo.request.dailyPlanTask.DailyTaskPageRequest;
import com.zhanghu.backendZray.pojo.request.OnlyIdRequest;
import com.zhanghu.backendZray.pojo.vo.DailyTaskVo;
import com.zhanghu.backendZray.service.DailyTaskService;
import com.zhanghu.backendZray.mapper.DailyTaskMapper;
import com.zhanghu.backendZray.pojo.entity.DailyTask;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author ZhangDong
 * @date 2024/7/10 17:32
 */
@Slf4j
@Service
@AllArgsConstructor
public class DailyTaskServiceImpl extends ServiceImpl<DailyTaskMapper, DailyTask> implements DailyTaskService {

    @Override
    public Page<DailyTaskVo> page(DailyTaskPageRequest request) {
        Page<DailyTaskVo> page = new Page<>(request.getPageNum(), request.getPageSize());
        Page<DailyTaskVo> list = baseMapper.page(page, request);
        return list;
    }

    @Override
    public void finishTask(OnlyIdRequest request) {
        UpdateWrapper<DailyTask> updateWrapper = new UpdateWrapper<DailyTask>()
                .eq("id", request.getId().intValue())
                .eq("status", 0)
                .set("status", 1);
        boolean result = this.update(updateWrapper);
        if (!result) {
            throw new ApiException(ApiResponse.Status.SERVICE_FAILED, "当前状态不支持完成任务");
        }
    }
}
